package com.fangxing.configs;

import com.fangxing.configs.shiro.MyShiroRealm;
import org.apache.shiro.authz.ModularRealmAuthorizer;
import org.apache.shiro.realm.Realm;
import org.apache.shiro.spring.web.ShiroFilterFactoryBean;
import org.apache.shiro.web.mgt.DefaultWebSecurityManager;
import org.springframework.context.annotation.Bean;

import java.util.*;

public class ShiroConfig {

    @Bean
    public ShiroFilterFactoryBean shirFilter(DefaultWebSecurityManager securityManager) {
        ShiroFilterFactoryBean shiroFilterFactoryBean = new ShiroFilterFactoryBean();

        // 必须设置 SecurityManager
        shiroFilterFactoryBean.setSecurityManager(securityManager);

        // 如果不设置默认会自动寻找Web工程根目录下的"/login.jsp"页面
        shiroFilterFactoryBean.setLoginUrl("/login");
        // 登录成功后要跳转的链接
        shiroFilterFactoryBean.setSuccessUrl("/index");
        // 未授权界面;
        shiroFilterFactoryBean.setUnauthorizedUrl("/403");

        // 拦截器.
        Map<String, String> filterChainDefinitionMap = new LinkedHashMap<String, String>();
        // 配置不会被拦截的链接 顺序判断
        filterChainDefinitionMap.put("/static/**", "anon");
        filterChainDefinitionMap.put("/ajaxLogin", "anon");

        // 配置退出过滤器,其中的具体的退出代码Shiro已经替我们实现了
        filterChainDefinitionMap.put("/logout", "logout");

        filterChainDefinitionMap.put("/add", "perms[权限添加]");

        // <!-- 过滤链定义，从上向下顺序执行，一般将 /**放在最为下边 -->:这是一个坑呢，一不小心代码就不好使了;
        // <!-- authc:所有url都必须认证通过才可以访问; anon:所有url都都可以匿名访问-->
        filterChainDefinitionMap.put("/**", "authc");

        shiroFilterFactoryBean.setFilterChainDefinitionMap(filterChainDefinitionMap);
        System.out.println("Shiro拦截器工厂类注入成功");
        return shiroFilterFactoryBean;
    }

    @Bean(name="securityManager")
    public DefaultWebSecurityManager getDefaultWebSecurityManage(){
        DefaultWebSecurityManager securityManager =  new DefaultWebSecurityManager();

        //Map<String, Object> shiroAuthenticatorRealms = new HashMap<>();
        //shiroAuthenticatorRealms.put("adminShiroRealm", adminShiroRealm());
        //shiroAuthenticatorRealms.put("customShiroRealm", customShiroRealm());

        //Collection<Realm> shiroAuthorizerRealms = new ArrayList<Realm>();
        //shiroAuthorizerRealms.add(adminShiroRealm());
        //shiroAuthorizerRealms.add(customShiroRealm());

        //CustomModularRealmAuthenticator customModularRealmAuthenticator = new CustomModularRealmAuthenticator();
        //customModularRealmAuthenticator.setDefinedRealms(shiroAuthenticatorRealms);
        //customModularRealmAuthenticator.setAuthenticationStrategy(authenticationStrategy());
        //securityManager.setAuthenticator(customModularRealmAuthenticator);
        //ModularRealmAuthorizer customModularRealmAuthorizer = new ModularRealmAuthorizer();
        //customModularRealmAuthorizer.setRealms(shiroAuthorizerRealms);
        //securityManager.setAuthorizer(customModularRealmAuthorizer);
        //注入缓存管理器;
        //securityManager.setCacheManager(redisCacheManager());
        //securityManager.setSessionManager(defaultWebSessionManager());
        return securityManager;
    }

    /**
     * 身份认证realm; (这个需要自己写，账号密码校验；权限等)
     *
     * @return
     */
    @Bean
    public MyShiroRealm myShiroRealm() {
        MyShiroRealm myShiroRealm = new MyShiroRealm();
        return myShiroRealm;
    }

}
